class SessionController < ApplicationController
  def new
  end

  def create
    if params[:session][:name].present?
      @user = User.find_by(name: params[:session][:name])
    elsif params[:session][:email].present?
      @user = User.find_by(email: params[:session][:email])
    else
      @user = nil
    end
    if @user && @user.authenticate(params[:session][:password])
      log_in @user
      redirect_to root_url
    else
      flash.now[:danger] = '账号密码错误'
      render 'new'
    end
  end

  def destroy
    log_out if logged_in?
  	redirect_to root_url
  end
end
